/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.swing.swixml.adapter;

import java.util.Locale;

import nemos.swing.ConvertException;
import nemos.swing.IConverter;
import nemos.swing.swixml.customization.NemosEngine;

import org.swixml.Converter;
import org.swixml.Localizer;
import org.swixml.SwingEngine;
import org.swixml.dom.Attribute;

/**
 * ConverterAdapter : The SwiXML adapter for OSGi converters.
 * 
 * <p>
 * This class adapts {@link IConverter} service into a SwiXML {@link Converter}.
 * </p>
 * 
 * @author Romain DUBOIS
 */
public class ConverterAdapter implements Converter<Object> {

    /** The adapted converter */
    private final IConverter mAdapted;

    /**
     * Constructor.
     * 
     * @param pAdapted
     *            The converter to adapt
     */
    public ConverterAdapter(final IConverter pAdapted) {
        super();

        mAdapted = pAdapted;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.swixml.Converter#convert(java.lang.Class,
     *      org.swixml.dom.Attribute, org.swixml.Localizer)
     */
    @SuppressWarnings({ "unused", "deprecation", "javadoc" })
    @Override
    public Object convert(final Class<?> type, final Attribute attr, final Localizer localizer)
            throws Exception {
        throw new UnsupportedOperationException();
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.swixml.Converter#convert(java.lang.Class,
     *      org.swixml.dom.Attribute, org.swixml.SwingEngine)
     */
    @Override
    public Object convert(final Class<?> type, final Attribute attr, final SwingEngine<?> engine)
            throws ConvertException {

        final Locale lLocale = ((NemosEngine<?>) engine).getLocalizer().getLocale();
        return mAdapted.convert(attr.getValue(), type, attr.getLocalName(), lLocale);
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.swixml.Converter#convertsTo()
     */
    @Override
    public Class<?> convertsTo() {
        return mAdapted.getType();
    }
}
